﻿
using Dapper;
using HBase;
using System.Collections.Generic;
using System;
using System.Data; 

namespace hCommand
{
    //一个任务完成则不允许执行删除任务
    public class DeleteTaskBSEngine : GkTransaction {
        protected override String getTransKey() {
            return "delete_task";
        }
        public static TaskDelete taskDelete;
        protected override String p() {
            return paramId +"";
        }
        public long paramId;
        protected CoreTask ct;

        protected CoreTaskParam param;
        //入库完成反馈
        protected override TransRes Execute( ) {
            param = getById<CoreTaskParam>(paramId );
            ct = TransUtil.getCoreTask(this ,param.taskId);
            String sql1 = @"select * from core_stock where id=@id ";
            AbsCoreStock cs = queryUnique<AbsCoreStock>(sql1, new { id=ct.stockId }) ;
            if (cs == null) {
                throw new Exception(paramId + "无有效记录");
            }
         
            taskDeleteUpdateStock( cs ,param.src);
  
            exec( "update Core_Task set Status=-1, Finsh_Time=sgj_get_sys_now where id=@id " ,ct);
            exec( "update Core_Task_Param set Status=-1 where task_Id=@id " ,ct);
            LocCoreUtil.UpdataWhLoc(this ,param.src , cs.deepDiff);
            LocCoreUtil.UpdataWhLoc(this ,param.des ,0);

            return new TransRes();
        }


         public void taskDeleteUpdateStock(  AbsCoreStock cs ,int src) {
            long id = cs.id;
          
            int status = cs.status;
            int locId = src;
            if (!I18nConfig.isLoc(src)) {
                status = (int)StockStatus.库外;
            } else if (I18nConfig.isLoc(src)) {
                status = (int)StockStatus.库存;
            } else {
                throw new Exception("未知任务类型" + id);
            }
            cs.status = status;
            string sql = @"update Core_stock set Status=@status,loc_Id=@locId, change_Time=sgj_get_sys_now where id=@id ";
            exec(  sql, new { id, status, locId });
             
            if (taskDelete != null)
            {
                taskDelete.delete(this, cs, status);
            }


        }

      

    }
}
